/**
 * window.localStorage 浏览器永久缓存
 * @method set 设置永久缓存
 * @method get 获取永久缓存
 * @method remove 移除永久缓存
 * @method clear 移除全部永久缓存
 */
export const Local = {
	// 设置永久缓存
	set(key, val, needEncode = false,expired = 120) {
		if (typeof val != 'string')
			val = JSON.stringify(val)
		if (needEncode) {
			val = encodeURIComponent(val)
			window.localStorage.setItem(key, val);
		} else {
			window.localStorage.setItem(key, val);
		}
		if (expired) {
			window.localStorage.setItem(`${key}__expires__`, `${Date.now() + 1000 * 60 * expired}`);
		}
	},
	// 获取永久缓存
	get(key, needParse = true, needEncode = false) {
		const expired = window.localStorage.getItem(`${key}__expires__`) || Date.now() + 1;
		const now = Date.now();

		if (now >= expired) {
			window.localStorage.removeItem(key);
			window.localStorage.removeItem(`${key}__expires__`);
			return null;
		}
		// else{ //应该是绝对过期时间，
		// 	window.localStorage.setItem(`${key}__expires__`, `${now + 1000 * 60 * 10}`);
		// }
		let json = window.localStorage.getItem(key);
		if (needEncode)json = decodeURIComponent(json)
		if (needParse)
			return JSON.parse(json);
		else
			return json;
	},
	// 移除永久缓存
	remove(key) {
		window.localStorage.removeItem(key);
	},
	// 移除全部永久缓存
	clear() {
		window.localStorage.clear();
	},
};

/**
 * window.sessionStorage 浏览器临时缓存
 * @method set 设置临时缓存
 * @method get 获取临时缓存
 * @method remove 移除临时缓存
 * @method clear 移除全部临时缓存
 */
export const Session = {
	// 设置临时缓存
	set(key, val, expired = 60) {
		if (typeof val != 'string')
			val = JSON.stringify(val)
		window.sessionStorage.setItem(key, val);
		if (expired) {
			window.sessionStorage.setItem(`${key}__expires__`, `${Date.now() + 1000 * 60 * expired}`);
		}
	},
	// 获取临时缓存
	get(key, needParse = true) {
		const expired = window.sessionStorage.getItem(`${key}__expires__`) || Date.now() + 1;
		const now = Date.now();

		if (now >= expired) {
			window.sessionStorage.removeItem(key);
			return null;
		}
		// else{ //应该是绝对过期时间，
		// 	window.sessionStorage.setItem(`${key}__expires__`, `${now + 1000 * 60 * 10}`);
		// }
		let json = window.sessionStorage.getItem(key);
		if (needParse)
			return JSON.parse(json);
		else
			return json;
	},
	// 移除临时缓存
	remove(key) {
		window.sessionStorage.removeItem(key);
	},
	// 移除全部临时缓存
	clear() {
		window.sessionStorage.clear();
	},
};

export const ClientStorage = {

	// 设置新的本地数据，默认存在会话缓存！！
	set(key, val, isLocalStorage = false, needEncode = false, expired = 60) {
		//如果值是对象，需要转换为字符串
		if (typeof val != 'string')
			val = JSON.stringify(val)

		if (needEncode) {
			val = encodeURIComponent(val)
		}

		if (isLocalStorage) {
			if (expired == 60) {
				expired = 120// 默认两个小时过期
			}
			Local.set(key, val, expired)
		} else {
			Session.set(key, val, expired)
		}

	},
	// 移除某个值
	remove(key, isLocalStorage = false) {
		if (isLocalStorage) {
			Local.remove(key)
		} else {
			Session.remove(key)
		}
	},
	// 获取某个值
	get(key, isLocalStorage = false, needDecode = false) {
		let json = ''
		if (isLocalStorage) {
			json = Local.get(key, false)
		} else {
			json = Session.get(key, false)
		}
		if (!json) { //不存在！！
			return null
		}
		if (needDecode) {
			json = decodeURIComponent(json)
		}
		return JSON.parse(json)
	},



}
